package com.myjpa.generate;

import lombok.Getter;
import lombok.Setter;

/**
 * @author: yaoll
 * @date: 2020-09-04
 * @verison: 1.0
 */
@Getter
@Setter
public class ControllerGenerator {

	private StringBuilder buff;

	public void generate(GenerateContext ctx) {

		boolean withModel = ctx.isWithModel();
		String targetClass = withModel ? ctx.getModelClass() : ctx.getEntityClass();

		buff = new StringBuilder();
		buff.append("package " + ctx.getPkg() + ".controller;\n");
		buff.append("\n");

		buff.append("import ").append(ctx.getCriteriaPackage()).append(".JacksonCriteria;\n");
		buff.append("import ").append(ctx.getResponsePackage()).append(".MapResponse;\n");
		buff.append("import ").append(ctx.getResponsePackage()).append(".PagedResponse;\n");
		if (withModel) {
			buff.append("import " + ctx.getPkg() + ".model." + ctx.getModelClass() + ";").append("\n");
		} else {
			buff.append("import " + ctx.getPkg() + ".entity." + ctx.getEntityClass() + ";").append("\n");
		}
		buff.append("import " + ctx.getPkg() + ".service." + ctx.getServiceClass() + ";").append("\n");
		buff.append("import org.springframework.beans.factory.annotation.Autowired;").append("\n");
		buff.append("import org.springframework.web.bind.annotation.PostMapping;").append("\n");
		buff.append("import org.springframework.web.bind.annotation.RequestBody;").append("\n");
		buff.append("import org.springframework.web.bind.annotation.RequestMapping;").append("\n");
		buff.append("import org.springframework.web.bind.annotation.RestController;").append("\n");

		buff.append("@RestController").append("\n");
		buff.append("@RequestMapping(\"" + ctx.getMappingPath() + "\")").append("\n");
		buff.append("public class " + ctx.getControllerClass() + " {").append("\n");

		buff.append("\t").append("@Autowired").append("\n");
		buff.append("\t").append("private " + ctx.getServiceClass() + " service;").append("\n");
		buff.append("\t").append("").append("\n");

		buff.append("	@PostMapping(\"/query\")").append("\n");
		buff.append("	public PagedResponse<" + targetClass + "> query(@RequestBody JacksonCriteria criteria) {").append("\n");
		buff.append("		return service.query(criteria);").append("\n");
		buff.append("	}").append("\n");
		buff.append("\t").append("").append("\n");

		buff.append("	@PostMapping(\"/create\")").append("\n");
		buff.append("	public MapResponse create(@RequestBody " + targetClass + " param){").append("\n");
		buff.append("		return service.create(param);").append("\n");
		buff.append("	}").append("\n");
		buff.append("\t").append("").append("\n");

		buff.append("	@PostMapping(\"/update\")").append("\n");
		buff.append("	public MapResponse update(@RequestBody " + targetClass + " param){").append("\n");
		buff.append("		return service.update(param);").append("\n");
		buff.append("	}").append("\n");
		buff.append("\t").append("").append("\n");

		buff.append("	@PostMapping(\"/delete\")").append("\n");
		buff.append("	public MapResponse delete(@RequestBody " + targetClass + " param){").append("\n");
		buff.append("		return service.delete(param);").append("\n");
		buff.append("	}").append("\n");
		buff.append("\t").append("").append("\n");

		buff.append("}").append("\n");
	}
}
